﻿using CanDichVu.Db;
using CanDichVu.Service.Iservice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanDichVu.Service.Vervices
{
    public class ComentSer : IComent
    {
        private readonly EntitiesCanDichVu dbContext = new EntitiesCanDichVu();

        public int addorUpdate(Db.Comment entity)
        {
            try
            {
                dbContext.Entry(entity).State = entity.Id == 0 ? System.Data.EntityState.Added : System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return entity.Id;
            }
            catch
            {
                return 0;
            }
        }

        public List<Db.Comment> getListByPost(int cateid)
        {
            try
            {
                return dbContext.Comments.Where(p => p.Postid == cateid || cateid == 0).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<Db.Comment> getListByUser(int userid)
        {
            try
            {
                return dbContext.Comments.Where(p => p.Userid == userid || userid == 0).ToList();
            }
            catch
            {
                return null;
            }
        }

        public Db.Comment GetById(int id)
        {
            try
            {
                return dbContext.Comments.Where(p => p.Id == id).First();
            }
            catch { return null; }
        }

        public bool detele(int id)
        {
            try
            {
                var _entity = GetById(id);
                dbContext.Comments.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }
    }
}
